const express = require('express');
const jwt = require('jsonwebtoken');
// 导入秘钥
const { secret } = require('../config/global');
// 导入文件上传中间件
const { upload } = require('../utils/upload');
// 导入connect-multiparty中间件, 用于处理form-data类型的数据
// const multipart = require('connect-multiparty');
const { login, refreshToken, checkToken } = require('./backend/login');
// 角色管理
const { roleList, roleAdd, roleRemove, roleUpdate, roleTotal } = require('./backend/role');
// 菜单管理
const { menuList, menuAdd, menuUpdate, menuRemove } = require('./backend/menu');
// 管理员管理
const { adminList, adminAdd, adminUpdate, adminRemove, adminTotal } = require('./backend/admin');
// 员工管理
const { staffAdd, staffRemove, staffList, staffUpdate, staffTotal } = require('./backend/staff');
// 科室管理
const { departmentAdd, departmentRemove, departmentList, departmentUpdate, departmentTotal } = require('./backend/department');
// 职务管理
const { positionAdd, positionRemove, positionList, positionUpdate, positionTotal } = require('./backend/position');
// 患者管理
const { patientAdd, patientRemove, patientList, patientUpdate, patientTotal, patientSearch } = require('./backend/patient');
// 预约管理 
const { appointmentAdd, appointmentRemove, appointmentList, appointmentUpdate, appointmentTotal } = require('./backend/appointment');
// 财务管理
const { financeAdd, financeRemove, financeList, financeUpdate, financeTotal, financeStats } = require('./backend/finance');
// 日志管理
const { logList, logTotal } = require('./backend/log');
// 医疗机构管理
const { businessTotal, businessList, businessAdd, businessUpdate, businessRemove } = require('./backend/business');
// 轮播图管理
const { bannerTotal, bannerList, bannerAdd, bannerUpdate, bannerRemove } = require('./backend/banner');

/* 
// 地区管理
const { regionList, regionAdd, regionUpdate, regionRemove } = require('./backend/region');

// 内容分类管理
const { classifyList, classifyAdd, classifyUpdate, classifyRemove } = require('./backend/classify');
// 文章管理
const { postsList, postsAdd, postsUpdate, postsRemove } = require('./backend/posts');
// 评论管理
const { replyList, replyRemove } = require('./backend/reply');
// 会员管理
const { memberList, memberUpdate } = require('./backend/member'); */

// 图片上传
const { imgUpload } = require('./upload');
// 创建路由实例
const router = express.Router();

// 创建multipart中间件实例
// const multipartMiddleware = multipart();

// 管理员登录
router.post('/login', login);
// 刷新token
router.post('/refreshToken', refreshToken);
// 图片上传
router.post('/imgUpload', upload.single('img'), imgUpload);

// 注册路由中间件: 判断登录状态, 如果没有登录, 或者登录超时, 则拒绝响应
router.use(function (req, res, next) {
    // 获取自定义请求头authorization
    const { authorization: token } = req.headers;
    if (!token) {
        return res.send({
            code: 500,
            msg: '请通过自定义请求头Authorization传递登录凭证token',
            data: []
        })
    }
    // 验证登录凭证token
    try {
        const decoded = jwt.verify(token, secret);
        if (decoded) {
            const { id, rid, username, exp } = decoded;
            // 过期
            if (exp < parseInt(Date.now() / 1000)) {
                return res.send({
                    code: 403,
                    msg: '登录凭证token无效或者登录超时请重新登录',
                    data: []
                })
            }
            // 将登录信息通过请求对象, 继续向后传递
            req.adminLoginInfo = { id, rid, username }
            next();
        }
    } catch (err) {
        if (err) {
            return res.send({
                code: 403,
                msg: '登录凭证token无效或者登录超时请重新登录',
                data: []
            })
        }
    }

});

// 检测token是否有效
router.get('/checkToken', checkToken)

// 菜单管理
router.get('/menuList', menuList);
router.put('/menuAdd', menuAdd);
router.post('/menuUpdate', menuUpdate);
router.delete('/menuRemove', menuRemove);

// 角色管理
router.get('/roleList', roleList);
router.get('/roleTotal', roleTotal);
router.put('/roleAdd', roleAdd);
router.post('/roleUpdate', roleUpdate);
router.delete('/roleRemove', roleRemove);


// 管理员管理
router.get('/adminList', adminList);
router.put('/adminAdd', upload.single('avatar'), adminAdd);
router.post('/adminUpdate', upload.single('avatar'), adminUpdate);
router.delete('/adminRemove', adminRemove);
router.get('/adminTotal', adminTotal);

// 员工管理
router.get('/staffList', staffList);
router.get('/staffTotal', staffTotal);
router.put('/staffAdd', upload.single('avatar'), staffAdd);
router.delete('/staffRemove', staffRemove);
router.post('/staffUpdate', upload.single('avatar'), staffUpdate);


// 科室管理
router.put('/departmentAdd', departmentAdd);
router.delete('/departmentRemove', departmentRemove);
router.post('/departmentUpdate', departmentUpdate);
router.get('/departmentList', departmentList);
router.get('/departmentTotal', departmentTotal);
// 职务管理
router.put('/positionAdd', positionAdd);
router.delete('/positionRemove', positionRemove);
router.post('/positionUpdate', positionUpdate);
router.get('/positionList', positionList);
router.get('/positionTotal', positionTotal);
// 患者管理
router.put('/patientAdd', patientAdd);
router.delete('/patientRemove', patientRemove);
router.post('/patientUpdate', patientUpdate);
router.get('/patientList', patientList);
router.get('/patientTotal', patientTotal);
router.get('/patientSearch', patientSearch);

// 预约管理
router.put('/appointmentAdd', appointmentAdd);
router.delete('/appointmentRemove', appointmentRemove);
router.post('/appointmentUpdate', appointmentUpdate);
router.get('/appointmentList', appointmentList);
router.get('/appointmentTotal', appointmentTotal);
// 财务管理
router.put('/financeAdd', financeAdd);
router.delete('/financeRemove', financeRemove);
router.post('/financeUpdate', financeUpdate);
router.get('/financeList', financeList);
router.get('/financeTotal', financeTotal);
router.get('/financeStats', financeStats);

// 日志管理
router.get('/logList', logList);
router.get('/logTotal', logTotal);

// 医疗机构管理
router.get('/businessTotal', businessTotal)
router.get('/businessList', businessList)
router.put('/businessAdd', upload.single('logo'), businessAdd)
router.post('/businessUpdate', upload.single('logo'), businessUpdate)
router.delete('/businessRemove', businessRemove)


// 轮播图管理
router.get('/bannerTotal', bannerTotal);
router.get('/bannerList', bannerList);
router.put('/bannerAdd', upload.single('img'), bannerAdd);
router.post('/bannerUpdate', upload.single('img'), bannerUpdate);
router.delete('/bannerRemove', bannerRemove);

/* 

// 地区管理
router.get('/regionList', regionList);
router.put('/regionAdd', regionAdd);
router.post('/regionUpdate', regionUpdate);
router.delete('/regionRemove', regionRemove);

// 内容分类管理
router.get('/classifyList', classifyList);
router.put('/classifyAdd', classifyAdd);
router.post('/classifyUpdate', classifyUpdate);
router.delete('/classifyRemove', classifyRemove);

// 内容管理
router.get('/postsList', postsList);
router.put('/postsAdd', upload.single('img'), postsAdd);
router.post('/postsUpdate', upload.single('img'), postsUpdate);
router.delete('/postsRemove', postsRemove);

// 评论管理
router.get('/replyList', replyList);
router.delete('/replyRemove', replyRemove);

// 会员管理
router.get('/memberList', memberList);
router.delete('/memberUpdate', memberUpdate); */


// 导出路由实例
module.exports = router;